package com.sk.ccflower.server.common.xmlRpcManager;

import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.ServletWebServer;

import com.sk.ccflower.lib.define.BaseCfg;
import com.sk.ccflower.server.lib.dbUtils.ConnectionPoolManager;
import com.sk.ccflower.server.lib.sysUtils.BaseMain;

public class XmlRpcManager extends BaseMain
{
	
	public ConnectionPoolManager mConnectionPool = null;
	
	public XmlRpcManager() throws Exception
	{
		
	}
	
	public void init( String[] args ) throws Exception
	{
		mConnectionPool = ConnectionPoolManager.getInstance(BaseCfg.JDBC_URL, BaseCfg.JDBC_USER, BaseCfg.JDBC_PWD);
	}
	
	public void start() throws Exception
	{
		try
		{
			MyXmlRpcServlet servlet = new MyXmlRpcServlet( mConnectionPool, this.getClass().getSimpleName() + ".properties" );
			ServletWebServer webServer = new ServletWebServer( servlet, BaseCfg.XMLRPC_PORT );
			
	        XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) servlet.getXmlRpcServletServer().getConfig();
	        serverConfig.setEnabledForExtensions( true );
	        serverConfig.setContentLengthOptional( true );	//  test
	        serverConfig.setBasicEncoding( "UTF-8" );
	        
			webServer.start();
			
			while ( true ) {
				m_log.debug( "=================== DBCP Information ===================" );
				m_log.debug( "### connectionPool.getNumActive() : "+mConnectionPool.getNumActive() );
				m_log.debug( "### connectionPool.getNumIdle() : "+mConnectionPool.getNumIdle() );
				m_log.debug( "========================================================" );
				Thread.sleep( (10*1000) );
			}
		}
		catch (Exception e)
		{
			this.m_log.error("XmlRpcManager: " + e.toString());
			throw new Exception(e);
		}
	}
	
    public static void main(String[] args)
    {
    	try
    	{
	    	XmlRpcManager server = new XmlRpcManager();
	    	server.init( args );
	    	server.start();
    	}
    	catch (Exception e)
    	{
    		e.printStackTrace();
    	}
    }
}
